package com.cyz.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.cyz.dao.CoursedesignDao;
import com.cyz.dao.StudentDao;
import com.cyz.dao.TeacherDao;
import com.cyz.dao.UserDao;
import com.cyz.entity.CoursedesignEntity;
import com.cyz.entity.StudentEntity;
import com.cyz.entity.TeacherEntity;
import com.cyz.entity.UserEntity;
import com.cyz.service.CoursedesignService;
import com.cyz.vo.CourseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;





/**
 *
 *
 * @author chenyizhen
 * @email
 * @date 2023-12-26 14:18:30
 */
@RestController
@RequestMapping("coursedesign")
public class CoursedesignController {

    @Autowired
    CoursedesignDao coursedesignDao;

    @Autowired
    TeacherDao teacherDao;

    @Autowired
    StudentDao studentDao;

    @Autowired
    UserDao userDao;
    /**
     * 列表
     */
    @RequestMapping("/list")
    public List<CourseDto> list(){
        List<CoursedesignEntity> coursedesignEntities = coursedesignDao.selectList(new QueryWrapper<>());

        Map<String, CourseDto> map = new HashMap<>();
        List<CourseDto> collect = coursedesignEntities.stream().map(item -> {
            CourseDto courseDto = new CourseDto();
            Integer teacherId = item.getTeacherId();
            TeacherEntity teacherEntity = teacherDao.selectById(teacherId);
            String key = item.getDesignName() + "_" + teacherEntity.getName();
            StudentEntity studentEntity = studentDao.selectById(item.getStudentId());
            if (!map.containsKey(key)) {
                courseDto.setId(item.getId());
                courseDto.setCourseName(item.getDesignName());
                courseDto.setTeacherName(teacherEntity.getName());
                if (studentEntity != null){
                    courseDto.setStudentName(studentEntity.getName());
                }
                map.put(key, courseDto);
            } else {
                courseDto = map.get(key);
                String studentName = courseDto.getStudentName();
                if (studentName != null) {
                    courseDto.setStudentName(studentName + "、" + studentEntity.getName());
                } else {
                    courseDto.setStudentName(studentEntity.getName());
                }
            }

            return courseDto;
        }).distinct().collect(Collectors.toList());


        return collect;
    }

    @RequestMapping("/query/{id}")
    public CourseDto query(@PathVariable int id){
        CoursedesignEntity coursedesignEntity = coursedesignDao.selectById(id);

        List<CoursedesignEntity> coursedesignEntities = coursedesignDao.selectList(new QueryWrapper<CoursedesignEntity>().eq("design_name", coursedesignEntity.getDesignName()).eq("teacher_id", coursedesignEntity.getTeacherId()));

        String studentName = "";

        CourseDto courseDto = new CourseDto();
        courseDto.setId(id);
        courseDto.setCourseName(coursedesignEntity.getDesignName());
        courseDto.setTeacherName(teacherDao.selectById(coursedesignEntity.getTeacherId()).getName());
        for (CoursedesignEntity coursedesign : coursedesignEntities){
            StudentEntity studentEntity = studentDao.selectById(coursedesign.getStudentId());
            if (!studentName.equals("")){
                studentName += "、";
            }

            studentName += studentEntity.getName();
        }
        courseDto.setStudentName(studentName);

        return courseDto;
    }
    @RequestMapping("/update")
    public String update(String courseName, String teacherName,@RequestParam List<String> selectedStudents){
        for (String name : selectedStudents){
            StudentEntity studentEntity = studentDao.selectOne(new QueryWrapper<StudentEntity>().eq("name", name));
            TeacherEntity teacherEntity = teacherDao.selectOne(new QueryWrapper<TeacherEntity>().eq("name", teacherName));

            List<CoursedesignEntity> coursedesignEntities = coursedesignDao.selectList(new QueryWrapper<CoursedesignEntity>().eq("design_name", courseName)
                    .eq("teacher_id", teacherEntity.getId()));

            List<StudentEntity> collect = coursedesignEntities.stream().map(item -> {
                Integer studentId = item.getStudentId();
                return studentDao.selectById(studentId);
            }).collect(Collectors.toList());

            for (StudentEntity student : collect){
                if (!selectedStudents.contains(student.getName())){
                    //删除之前存在现在不存在的
                    coursedesignDao.delete(new QueryWrapper<CoursedesignEntity>().eq("design_name", courseName)
                            .eq("student_id",student.getId())
                            .eq("teacher_id",teacherEntity.getId()));
                }
            }

            CoursedesignEntity coursedesignEntity = coursedesignDao.selectOne(new QueryWrapper<CoursedesignEntity>().eq("design_name", courseName)
                    .eq("student_id", studentEntity.getId())
                    .eq("teacher_id", teacherEntity.getId()));
            if (coursedesignEntity == null){
                //新增之前没有的
                coursedesignEntity = new CoursedesignEntity();
                coursedesignEntity.setDesignName(courseName);
                coursedesignEntity.setStudentId(studentEntity.getId());
                coursedesignEntity.setTeacherId(teacherEntity.getId());
                coursedesignDao.insert(coursedesignEntity);
            }
        }

        return "123";
    }

    @RequestMapping("/insert/{account}")
    public String insert(@PathVariable String account, String courseName){
        UserEntity entity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("account", account));

        TeacherEntity teacherEntity = teacherDao.selectOne(new QueryWrapper<TeacherEntity>().eq("name", entity.getName()));

        CoursedesignEntity coursedesignEntity = new CoursedesignEntity();
        coursedesignEntity.setTeacherId(teacherEntity.getId());
        coursedesignEntity.setDesignName(courseName);
        coursedesignDao.insert(coursedesignEntity);
        return "添加成功";
    }

//
//
//    /**
//     * 信息
//     */
//    @RequestMapping("/info/{id}")
//    public R info(@PathVariable("id") Integer id){
//		CoursedesignEntity coursedesign = coursedesignService.getById(id);
//
//        return R.ok().put("coursedesign", coursedesign);
//    }
//
//    /**
//     * 保存
//     */
//    @RequestMapping("/save")
//    public R save(@RequestBody CoursedesignEntity coursedesign){
//		coursedesignService.save(coursedesign);
//
//        return R.ok();
//    }
//
//    /**
//     * 修改
//     */
//    @RequestMapping("/update")
//    public R update(@RequestBody CoursedesignEntity coursedesign){
//		coursedesignService.updateById(coursedesign);
//
//        return R.ok();
//    }
//
//    /**
//     * 删除
//     */
//    @RequestMapping("/delete")
//    public R delete(@RequestBody Integer[] ids){
//		coursedesignService.removeByIds(Arrays.asList(ids));
//
//        return R.ok();
//    }

}
